<?php
function getVolunteerMainStatus($st){
    $status=$st;
    if ($status=='active'){
        $new="Active";
    }
    elseif ($status=='inactive'){
        $new="Inactive";
    }
    else{
        $new="Not activated";
    }
    return $new;
}
function eventStatus($s){
    $status = $s;
    if ($status=='requested'){
        $newstatus = "New";
    } elseif ($status=='pending'){
        $newstatus = "Confirmed";
    } elseif ($status=='confirmed'){
        $newstatus='Finalized';
    } else {
        $newstatus = "Unknown";
    }
    return $newstatus;
}
function startend($sessionArray) {
    $session_array = $sessionArray;
    $first = $last = $session_array[0]['session_date'];
    for ($m=0;$m<count($session_array);$m++) {
        if ($session_array[$m]['session_date']<$first) {
            $first = $session_array[$m]['session_date'];
        }
        if ($session_array[$m]['session_date']>$last) {
            $last = $session_array[$m]['session_date'];
        }
    }
    $firstlast = Array ($first,$last);
    return $firstlast;
}
function newname($filename) {
    $a = $filename;
    $array = explode('.',$a);
    $n=count($array)-1;
    $name = rand (1,99999999999);
    $newname = $name.".".$array[$n];
    return $newname;
}
function dynamicSelectForm($default1,$valueArray,$displayArray) {
    $default= $default1;
    $value_array = $valueArray;
    $display_array = $displayArray;

    //check valid input
    if (count($value_array)!=count($display_array)) {
        echo "<b><front color ='red'>Your input is not valid</front></b>";
    } else {
        for ($i=0;$i<count($value_array);$i++) {
            $check ="";
            if ($default==$value_array[$i]) {
                $check="selected";
            }
            echo "<option value=$value_array[$i] ".$check.">$display_array[$i]</option>";
        }
    }
}
/* TEST DYNAMIC SELECT FORM
$defaulttest = 3;
$value = Array(0,1,2,3,4);
$display = Array("FedEx","Darlar","Minh","Thao","Ngoc");

echo "<select>";
    dynamicSelectForm($defaulttest,$value,$display);
echo "</select>";*/
function dynamicCheckboxForm($defaultArray,$valueArray,$displayArray,$name) {
    $defaul_array= $defaultArray;
    $value_array = $valueArray;
    $display_array = $displayArray;
    $nameinput = $name;
    if (count($value_array)!=count($display_array)) {
        echo "<b><front color ='red'>Your input is not valid</front></b>";
    } else {
        for ($i=0;$i<count($valueArray);$i++) {
            $value = $valueArray[$i];
            $check ="";
            for ($j=0;$j<count($defaul_array);$j++) {
                if ($defaul_array[$j]==$value) {
                    $check="checked";
                }
            }
            echo "<input type='checkbox' name=$nameinput value = $value $check>      ";
            echo $display_array[$i]."<br/>";
        }
    }
}
/* /* TEST DYNAMIC CHECKBOX FORM
$defaulttest = Array(2,3);
$value = Array(0,1,2,3,4);
$display = Array("FedEx","Darlar","Minh","Thao","Ngoc");
//dynamicCheckboxForm($defaulttest,$value,$display,"school[]");
*/
function getValueArray($originalArray,$columnName) {
    $column_name = $columnName;
    $original_array = $originalArray;
    $valueArray = Array();
    for ($i=0;$i<count($original_array);$i++) {
        $valueArray[]= ucwords($originalArray[$i][$column_name]);
    }
    return $valueArray;
}
/* /* TEST GET VALUE ARRAY
include "../include/dbFunctions.php";
$id=3;
//Data from education table for select form
    $query7= "SELECT education_id, education_name FROM education";
    $education_array_original = executeSelectQuery($query7);
    
    $query3 = "SELECT e.education_id, e.education_name
                FROM education e, education_preference p
                WHERE e.education_id = p.education_id
                AND p.volunteer_id = $id";
    $education_array = executeSelectQuery($query3);

$value_array = getValueArray($education_array_original,"education_name");
print_r($value_array);
*/
function menuViewPage($updateLink,$deleteLink,$moduleId) {
    $update_link = $updateLink;
    $delete_link = $deleteLink;
    $id = $moduleId;
    echo <<<HERE
    <table>
                <tr>
                    <td>
                        <form action="">
                            <input type="button" class="form-back" value="Back" onClick="javascript:history.go(-1)"/>
                        </form>
                    </td>
                    <td>
                         <form method="POST" action= $update_link id="submitform">
                            <input type="hidden" name="id" value=$id></input>
                            <input type="Submit" class="form-edit" name="Edit" value="Edit"/>
                        </form>
                    </td>
                    <td>
                         <form method="POST" action= $delete_link id="submitform">
                            <input type="hidden" name="id" value=$id></input>
                            <input type="Submit" class="form-cancel" name="Cancel" value="Delete" onClick="if (confirm('This event will be cancelled. Email notification will be sent to school and all trainers. Do you wish to continue?')) commentDelete(1); return false"/>
                        </form>
                    </td>
                </tr>
            </table>
HERE;
}
function menuViewPage2($updateLink,$deleteLink,$moduleId) {
    $update_link = $updateLink;
    $delete_link = $deleteLink;
    $id = $moduleId;
    echo <<<HERE
    <table>
                <tr>
                    <td>
                        <form action="">
                            <input type="button" class="form-back" value="Back" onClick="javascript:history.go(-1)"/>
                        </form>
                    </td>
                    <td>
                         <form method="POST" action= $update_link id="submitform">
                            <input type="hidden" name="id" value=$id></input>
                            <input type="Submit" class="form-edit" name="Edit" value="Edit"/>
                        </form>
                    </td>
                    <td>
                         <form method="POST" action= $delete_link id="submitform">
                            <input type="hidden" name="id" value=$id></input>
                            <input type="Submit" class="form-delete" name="Delete" value="Delete" onClick="if (confirm('This event will be deleted permanently. Do you wish to continue?')) commentDelete(1); return false"/>
                        </form>
                    </td>
                </tr>
            </table>
HERE;
}
function asort2d($records, $field, $reverse) {
// Sort an array of arrays with text keys, like a 2d array from a table query:
    $hash = array();
    foreach($records as $key => $record) {
        $hash[$record[$field].$key] = $record;
    }
    ($reverse)? krsort($hash) : ksort($hash);
    $records = array();
    foreach($hash as $record) {
        $records []= $record;
    }
    return $records;
}
function getEventStatus($start,$end) {
    $start_date = $start;
    $end_date = $end;
    $today = date("Y-m-d");
    if(($start_date>$today)) {
        $newstatus="Upcoming";
    } elseif ($end_date>$today) {
        $newstatus ="Ongoing";
    } elseif ($end<$today) {
        $newstatus="Past";
    }
    return $newstatus;
}
function getVolunteerStatus($status) {
    if ($status=="requested") {
        $newstatus = "Waiting For Acceptance";
    }
    elseif ($status=="accept") {
        $newstatus = "Waiting For Class Assignment";
    }
    elseif ($status=="confirmed") {
        $newstatus = "Confirmed For Event & Class";
    } elseif ($status=="cancelled") {
        $newstatus = "Request Rejected";
    } else {
        $newstatus = "None";
    }
    return $newstatus;
}
function getMatchStatus($aStatus,$vid) {
    $status = $aStatus;
    if ($status=="match") {
        $status ="<a href= 'eventVolunteerDetail.php?id=$vid'><font size='0' color ='white'>1</font><img src='../media/images/forms/check_icon.png' height ='20'></a>";
    } elseif ($status=="partial") {
        $status = "<a href= 'eventVolunteerDetail.php?id=$vid'><font size='0' color ='white'>2</font><img src='../media/images/forms/partial_icon.png' height ='20'></a>";
    } else {
        $status = "<a href= 'eventVolunteerDetail.php?id=$vid'><font size='0' color ='white'>3</font><img src='../media/images/forms/cross_icon.png' height ='20'></a>";
    }
    return $status;
}

function getOpenStatus($aStatus) {
    $status = $aStatus;
    if ($status=="requested") {
        $newStatus = "<font color='red'>New</font>";
    }
    elseif ($status=="pending") {
        $newStatus = "Pending Trainer";
    }
    return $newStatus;
}
function calculateEndTime($startTime, $duratioN) {
    $starttime = $startTime;
    $duration = $duratioN;
    $start_array = explode(":",$starttime);
    $add_array = explode(":",$duration);

    $hour1 = $start_array[0];
    $min1 = $start_array[1];

    $houradd = intval($add_array[0]);
    $minadd = intval($add_array[1]);

    $newmin = intval(($min1+$minadd)%60);
    $newhour = ($hour1+$houradd+intval(($min1+$minadd)/60));
    if ($newmin>=10) {
        $end = strval($newhour).":".strval($newmin);
    } else {
        $end = strval($newhour).":0".strval($newmin);
    }return




            $end;
}
function icon($value) {
    $va = $value;
    if ($va=='match') {
        $print = "<img src='../media/images/forms/check_icon.png' height ='20'>";
    } elseif ($va=='not') {
        $print = "<img src='../media/images/forms/cross_icon.png' height ='20'>";
    }
    return $print;
}


?>